package com.jwdf.uda.ui.adapter;

import android.content.Context;
import android.databinding.DataBindingUtil;
import android.support.v4.app.FragmentActivity;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import com.jwdf.uda.R;
import com.jwdf.uda.bean.PbBean;
import com.jwdf.uda.databinding.ItemConcernRecommandBinding;
import com.jwdf.uda.http.AHttpService;
import com.jwdf.uda.http.HttpHelper;
import com.jwdf.uda.http.action.UserAddFollowAction;
import com.jwdf.uda.http.action.UserCancelFollowAction;
import com.jwdf.uda.ui.activity.UserInfoActivity;
import com.jwdf.uda.ui.fragment.base.BaseFragment;
import com.jwdf.uda.utils.ConfigUtils;
import com.jwdf.uda.utils.StringUtils;
import com.nostra13.universalimageloader.core.ImageLoader;
import java.util.List;
import retrofit2.Response;
/**
 * Created by fatsfan on 2016/10/11.
 */

public class ActorAdapter extends AbsAdapter<PbBean.PbUserSimple> {

    private BaseFragment fragment;

    public ActorAdapter(Context context, BaseFragment fragment) {
        super(context);
        this.fragment = fragment;
    }

    public List<PbBean.PbUserSimple> getData() {
        return mData;
    }

    @Override
    protected AbsViewHolder onCreateViewHolder(ViewGroup parent, int itemType) {
        return new ItemQuizRecommandHolder(mInflater.inflate(R.layout.item_concern_recommand, parent, false));
    }

    private class ItemQuizRecommandHolder extends AbsViewHolder<PbBean.PbUserSimple> {
        ItemConcernRecommandBinding mBinding;
        public ItemQuizRecommandHolder(View convertView) {
            super(convertView);
            mBinding = DataBindingUtil.bind(convertView);
        }

        @Override
        protected void onBindView(PbBean.PbUserSimple item) {
            mBinding.tvName.setText(item.getNickname());
            mBinding.tvInfo.setVisibility(TextUtils.isEmpty(StringUtils.filterNullAndTrim(item.getIntroduction())) ? View.GONE:View.VISIBLE);
            mBinding.tvInfo.setText(item.getIntroduction());

            ImageLoader.getInstance().displayImage(item.getAvatar().getUrl(), mBinding.icon);

            final PbBean.PbSignUserFollowType followType = item.getFollowType();
            final View vSel = mBinding.vSel;
            if(followType == PbBean.PbSignUserFollowType.USER_FOLLOW_TYPE_FOLLOW)
                vSel.setSelected(true);
            else
                vSel.setSelected(false);

            vSel.setOnClickListener(this);

            mBinding.vContent.setOnClickListener(this);

            if(item.getIsDesigner())
                mBinding.vIsDesigner.setVisibility(View.VISIBLE);
            else
                mBinding.vIsDesigner.setVisibility(View.GONE);

        }

        @Override
        public void onClick(final View v) {
            switch(v.getId()) {

                case R.id.vContent:
                    UserInfoActivity.start(v.getContext(), item.getUserId(), item.getIsDesigner());
                    break;

                case R.id.vSel:
//                    fragment.showProgressDialog();

                    final FragmentActivity aty = fragment.getActivity();

                    if(v.isSelected())
                        fragment.onNewRequestCall(UserCancelFollowAction.newInstance(aty, item.getUserId()).request(new AHttpService.IResCallback<PbBean.PbResUserDelFollow>() {
                            @Override
                            public void onCallback(int resultCode, Response<PbBean.PbResUserDelFollow> response, PbBean.PbBaseServerInfo baseServerInfo) {
                                if(fragment.getView() != null) {
//                                    fragment.closeProgressDialog();
                                    if(HttpHelper.showError(fragment.getContext(), resultCode, baseServerInfo, fragment.getString(R.string.error_net))) {
                                        return;
                                    }
                                }

                                v.setSelected(false);
                                ConfigUtils.reduceFollowCount(aty);
                            }
                        }));

                    else
                        fragment.onNewRequestCall(UserAddFollowAction.newInstance(aty, item.getUserId()).request(new AHttpService.IResCallback<PbBean.PbResUserAddFollow>() {
                            @Override
                            public void onCallback(int resultCode, Response<PbBean.PbResUserAddFollow> response, PbBean.PbBaseServerInfo baseServerInfo) {
                                if(fragment.getView() != null) {
                                    fragment.closeProgressDialog();
                                    if(HttpHelper.showError(fragment.getContext(), resultCode, baseServerInfo, fragment.getString(R.string.error_net))) {
                                        return;
                                    }
                                }

                                v.setSelected(true);
                                ConfigUtils.addFollowCount(aty);
                            }
                        }));

                    break;
            }

        }
    }
}
